package com.xinzhiduming.mp3palyer;

import android.R.integer;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseOperator {
	
	public static void insertData(String type, String data, Context context){
		ContentValues values = new ContentValues();
		DatabaseHelper databaseHelper = new DatabaseHelper(context, AppConstant.TABLE_NAME);
		SQLiteDatabase sqLiteDatabase = databaseHelper.getWritableDatabase();
		values.clear();
		values.put(type, data);
		if (sqLiteDatabase.update(AppConstant.TABLE_NAME, values, null, null) == 0) {
			sqLiteDatabase.insert(AppConstant.TABLE_NAME, null, values);
		}
		sqLiteDatabase.close();
	}
	
	public static String getData(String type, Context context){
		String res = "";
		DatabaseHelper databaseHelper = new DatabaseHelper(context, AppConstant.TABLE_NAME);
		SQLiteDatabase sqLiteDatabase = databaseHelper.getWritableDatabase();
		Cursor cursor = sqLiteDatabase.query(AppConstant.TABLE_NAME, new String[] {type}, null, null, null, null, null);
		while (cursor.moveToNext())  {
			res = cursor.getString(cursor.getColumnIndex(type)); 
		}
		cursor.close();
		sqLiteDatabase.close();
		return res;
	}
	
	public static boolean isMustHasHeadSet(Context context){   // 1=true 2=false
		boolean isMust = true;
		int isMust_ = 1;
		DatabaseHelper databaseHelper = new DatabaseHelper(context, AppConstant.TABLE_NAME);
		SQLiteDatabase sqLiteDatabase = databaseHelper.getWritableDatabase();
		Cursor cursor = sqLiteDatabase.query(AppConstant.TABLE_NAME, new String[] {"MUST_HAS_HEAD_SET"}, null, null, null, null, null);
		while (cursor.moveToNext())  {
			isMust_ = cursor.getInt(cursor.getColumnIndex("MUST_HAS_HEAD_SET")); 
		}
		if (isMust_ == 2) {
			isMust = false;
		}else if (isMust_ == 1) {
			isMust = true;
		}
		cursor.close();
		sqLiteDatabase.close();
		return isMust;
	}
	
	public static void setHeadStatusInDB(int status, Context context) {
		ContentValues values = new ContentValues();
		DatabaseHelper databaseHelper = new DatabaseHelper(context, AppConstant.TABLE_NAME);
		SQLiteDatabase sqLiteDatabase = databaseHelper.getWritableDatabase();
		values.clear();
		values.put("MUST_HAS_HEAD_SET", status);
		if (sqLiteDatabase.update(AppConstant.TABLE_NAME, values, null, null) == 0) {
			sqLiteDatabase.insert(AppConstant.TABLE_NAME, null, values);
		}
		sqLiteDatabase.close();		
	}
}